package cn.gok.mybatisplus.controller;

import cn.gok.mybatisplus.entity.Clazz;
import cn.gok.mybatisplus.service.ClazzService;
import cn.gok.mybatisplus.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName ClazzController
 * @Description TODO
 * @Author HP
 * @Date 17:11 2022/7/21
 * @Version 1.0
 **/
@Controller
@RequestMapping("clazz")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private Clazz clazz;
    private ModelAndView modelAndView = new ModelAndView();

    //查询所有班级信息
    @RequestMapping("/selectAll")
    public ModelAndView selectAll() {
        List<Clazz> list = clazzService.list();
        modelAndView.addObject("clazzList", list);
        modelAndView.addObject("message", "找到" + list.size() + "条记录");
        modelAndView.setViewName("clazzList");
        return modelAndView;
    }

    //根据id查询班级信息
    @RequestMapping("/selById")
    public ModelAndView selById(@RequestParam Long cid) {
        System.out.println(cid);
        if (cid == null) {
            modelAndView.setViewName("redirect:/clazz/selectAll");
        } else {
            Clazz clazz = clazzService.getById(cid);
            modelAndView.addObject("clazzList", clazz);
            if (clazz == null) {
                modelAndView.addObject("message", "没有找到记录");
            } else {
                modelAndView.addObject("message", "找到1条记录");
            }
            modelAndView.setViewName("clazzList");
        }
        return modelAndView;
    }

    //添加或更新班级
    @RequestMapping("addOrUpdate")
    public ModelAndView add(@RequestParam String cname, @RequestParam Long cid) {
        cname = cname.substring(cname.indexOf("=") + 1, cname.length());
        Clazz clazz = new Clazz();
        if (cid != null) {
            clazz.setCid(cid);
        }
        clazz.setCname(cname);
        try {
            modelAndView.addObject("status_message", clazzService.saveOrUpdate(clazz) ? "新增或修改：" + cname : "操作失败");
        } catch (Exception e) {
            SQLException se = (SQLException) e.getCause();
            se.getErrorCode();
            modelAndView.addObject("status_message", "非法id！");
        } finally {
            modelAndView.setViewName("redirect:/clazz/selectAll");
            return modelAndView;
        }
    }

    //根据id删除班级
    @RequestMapping("del")
    public ModelAndView del(Long cid) {
        clazzService.removeById(cid);
        modelAndView.addObject("status_message", "删除：" + cid);
        modelAndView.setViewName("redirect:/clazz/selectAll");
        return modelAndView;
    }

    //以下内容需手动访问
    //查询所有班级信息和对应的学生信息
    @RequestMapping("selectAllClazzAndStudent")
    @ResponseBody
    public String selectAllClazzAndStudent() {
        StringBuffer str = new StringBuffer();
        List<Clazz> clazzes = clazzService.list();
        for (Clazz clazz : clazzes) {
            str.append(clazz);
            str.append("<br/>");
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("cid", clazz.getCid());
            str.append(studentService.listMaps(queryWrapper));
            str.append("<br/>-----------------------------<br/>");
        }
        return str.toString();
    }

    //批量删除班级
    @RequestMapping(value = "/delClazzs")
    @ResponseBody
    public String delClazzs(List<Long> ids) {
        return clazzService.removeByIds(ids) ? "删除成功" : "删除失败";
    }



}
